<?php
namespace App\Repositories;
use App\Models\PrtAreas;
use App\Services\Helper;
/**
 * 仓库模板
 * 仓库模板
 * @author:
 * @version: 1.0
 * @date:
 */
class PrtAreasRepository extends BaseRepository
{
    protected $isCache = false; //是否使用缓存

    public function __construct(PrtAreas $model)
    {
        $this->model =$model;
    }

    /**
     * 获取列表记录
     * @param null $where
     * @param null $order
     * @return mixed
     */
    public function getTableList($where=null, $order=null)
    {
        $limit = isset($where['limit']) ? $where['limit']:config('common.page_limit');  //这个10取配置里的
        $where = $this->parseWhere($where);

        //order 必须以 'id desc'这种方式传入.
        $orderBy = [];
        if (!empty ($order)) {
            $arrOrder = explode(' ', $order);
            if(count($arrOrder) == 2) {
                $orderBy = $arrOrder;
            }
        }
        $query = $this->model;
         //创建时间查询
        if(isset($where['created_at']) && !empty($where['created_at'])){
            $created_at = $where['created_at'];
            $time_list = Helper::getTimeRangedata($created_at);
            $query = $query->whereBetween("created_at",[$time_list['start'],$time_list['end']]);
            unset($where['created_at']);
        }
        //更新时间查询
        if(isset($where['updated_at']) && !empty($where['updated_at'])){
            $updated_at = $where['updated_at'];
            $time_list = Helper::getTimeRangedata($updated_at);
            $query = $query->whereBetween("updated_at",[$time_list['start'],$time_list['end']]);
            unset($where['updated_at']);
        }
        if (isset($where['sys_code']) && !empty($where['sys_code'])) {
            $sysCode = preg_replace("/[^0-9]/","",$where['sys_code']);
            $id = intval($sysCode);
            $query = $query->where('id',$id);
            unset($where['sys_code']);
        }

        if(!empty ($where)) {
            $query =  $query->where($where);
        }

        if(!empty($order)) {
            $t = count($arrOrder) / 2;
            for ($i = 0 ; $i< $t; $i++) {
                $query =  $query->orderBy($orderBy[2*$i],$orderBy[2*$i+1]);
            }
        }

        $list = $query->paginate($limit);
        return $list;
    }


    /**
     * 新增/修改
     * @param $data
     * @return boolean
     */
    public function save($data)
    {
        if(empty($data['id'])) {
            unset($data['id']);
            $data['created_at'] = time();
            $ret = $this->model->insertGetId($data);
            $priKeyValue = $ret;
        } else {
            $priKeyValue = $data['id'];
            unset($data['id']);
            $data['updated_at'] = time();
            $ret =$this->model->where('area_id',$priKeyValue)->update($data);
        }
        return $ret;

    }

    /**
     * 删除(软删除)
     * @param $id
     * @return bool
     */
    public function delete($id)
    {
        $this->model->where('area_id', $id)->delete();


        if( $this->model->trashed()){
            return true;
        }else{
            return true;
        }
    }
    /**
     *  获取地区的id并查找出pid
     *
     * @return array
     */

    public function getAreaIdList($id)
    {
        $pid =  $this->model->where('area_id',$id)->first();
        return $pid;
    }
    /**
     *  获取省市区地区的联动数组
     *
     * @return array
     */
    public function getAreasLists($id)
    {
        if($id != 0){
            $list = $this->model->where('pid',$id)->select('area_id', 'area_name')->get();
        }elseif ($id == -1){
            return '';
        }
        else{
            $list = $this->model->where('level',1)->select('area_id', 'area_name')->get();
        }

        return $list;
    }
}
